package com.health.life;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.google.myjson.Gson;
import com.health.life.enty.CookClass;
import com.health.life.enty.CookClassResp;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends MyBaseActivity {
    private ExpandableListView exListView;
    private com.google.myjson.Gson gson=new Gson();
    private boolean isSub=false;
    android.os.Handler handler=new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 2:
                    String receiveJson= (String) msg.obj;
                    CookClassResp response = gson.fromJson(receiveJson, CookClassResp.class);
                    List<CookClass> temList=response.getYi18();
                    DbUtils dbUtils=DbUtils.create(HomeActivity.this);
                    try {
                        dbUtils.saveOrUpdateAll(temList);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    if (temList==null||temList.size()<1)
                        break;
                    if (isSub)
                    {
                        listchild.clear();
                        listchild.addAll(temList);
                        mAdapter.notifyDataSetChanged();
                        break;
                    }
                        listParent.clear();
                    listParent.addAll(temList);
                    mAdapter.notifyDataSetChanged();
                    break;
                default:
                    showToaskMsg("ddfd");
            }
        }
    }
            ;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        exListView = (ExpandableListView) findViewById(R.id.exlistView);
        exListView.setAdapter(mAdapter);
        exListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if (exListView.isGroupExpanded(groupPosition))
                {
                    exListView.collapseGroup(groupPosition);
                    return false;
                }

                for (int i = 0; i < listParent.size(); i++) {
                    if (i!=groupPosition)
                        exListView.collapseGroup(i);
                }
//                exListView.expandGroup(groupPosition);
                isSub=true;
                getData(listParent.get(groupPosition).getId());

                return false;
            }
        });
        getData("");
    }

    public void getData(String id){


        new Thread(){
            @Override
            public void run() {
                super.run();
                String sub="";
                if (id!="")
                {
                    sub="?id="+id;
                }
                HttpUtils httpUtils=new HttpUtils();
                httpUtils.send(HttpRequest.HttpMethod.POST, App.COOKCLASS_URl+sub, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Message message=handler.obtainMessage();
                        message.obj=responseInfo.result;
                        message.what=2;
                        handler.sendMessage(message);
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        handler.sendEmptyMessage(4);
                    }
                });
            }
        }.start();
    }




    private List<CookClass> listParent=new ArrayList<>(0);
    private List<CookClass> listchild=new ArrayList<>(0);
    BaseExpandableListAdapter mAdapter=new BaseExpandableListAdapter() {
        @Override
        public int getGroupCount() {
            return listParent.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return listchild.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return listParent.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return listchild.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView textView= (TextView) LayoutInflater.from(HomeActivity.this).inflate(android.R.layout.simple_list_item_1,null,false);
            textView.setText(listParent.get(groupPosition).getName());
            return textView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            TextView textView= (TextView) LayoutInflater.from(HomeActivity.this).inflate(android.R.layout.simple_list_item_1,null,false);
            textView.setText(listchild.get(childPosition).getName());
            return textView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    };
}
